/*
 * CorbaSender.cpp
 *
 *  Created on: Mar 31, 2011
 *      Author: alban
 */

#include <iostream>
#include <XnCppWrapper.h>
#include "CorbaSender.h"

extern xn::DepthGenerator g_DepthGenerator;


CorbaSender::CorbaSender(int argc, char ** argv) {
  /* Create a CORBA ORB to request the server */
  CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

  try {
    /* Get the object by id on server and cast to the right type */
    CORBA::Object_var obj = orb->string_to_object(argv[1]);
    _dataref = KiBeam::Data::_narrow(obj);

    if (CORBA::is_nil(_dataref)) {
      std::cerr << "Can't narrow reference to type KiBeam::Data" << std::endl;
    }
  } catch (CORBA::TRANSIENT&) {
    std::cerr << "TRANSIENT Exception : Error on network" << std::endl
        << "Wrong server or connection malfunction" << std::endl;
  }
}

void CorbaSender::send(xn::UserGenerator g_UserGenerator) {
  XnUserID users[1];
  XnUInt16 n_users = 1;

  // get data for each users
  g_UserGenerator.GetUsers(users,n_users);
  try {
    for(int i = 0 ; i < n_users ; i++){
      if(g_UserGenerator.GetSkeletonCap().IsTracking(users[i])){
        XnSkeletonJointPosition p_head,
        p_neck,
        p_torso,
        p_waist,
        p_left_collar,
        p_left_shoulder,
        p_left_elbow,
        p_left_wrist,
        p_left_hand,
        p_left_fingertip,
        p_right_collar,
        p_right_shoulder,
        p_right_elbow,
        p_right_wrist,
        p_right_hand,
        p_right_fingertip,
        p_left_hip,
        p_left_knee,
        p_left_ankle,
        p_left_foot,
        p_right_hip,
        p_right_knee,
        p_right_ankle,
        p_right_foot;
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_HEAD,p_head);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_NECK,p_neck);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_TORSO,p_torso);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_WAIST,p_waist);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_COLLAR,p_left_collar);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_SHOULDER,p_left_shoulder);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_ELBOW,p_left_elbow);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_WRIST,p_left_wrist);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_HAND,p_left_hand);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_FINGERTIP,p_left_fingertip);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_COLLAR,p_right_collar);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_SHOULDER,p_right_shoulder);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_ELBOW,p_right_elbow);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_WRIST,p_right_wrist);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_HAND,p_right_hand);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_FINGERTIP,p_right_fingertip);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_HIP,p_left_hip);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_KNEE,p_left_knee);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_ANKLE,p_left_ankle);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_LEFT_FOOT,p_left_foot);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_HIP,p_right_hip);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_KNEE,p_right_knee);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_ANKLE,p_right_ankle);
        g_UserGenerator.GetSkeletonCap().GetSkeletonJointPosition(users[i],XN_SKEL_RIGHT_FOOT,p_right_foot);


      }
    }
  } catch (CORBA::TRANSIENT&) {
    std::cerr << "TRANSIENT Exception : Error on network" << std::endl
            << "Wrong server or connection malfunction" << std::endl;
  }
}

void CorbaSender::newUserEvent(unsigned long user_id){
  try{
    _dataref->newUserEvent((CORBA::ULong) user_id);
  } catch (CORBA::TRANSIENT&) {
    std::cerr << "TRANSIENT Exception : Error on network" << std::endl
        << "Wrong server or connection malfunction" << std::endl;
  }
}

void CorbaSender::lostUserEvent(unsigned long user_id){
  try{
    _dataref->lostUserEvent((CORBA::ULong) user_id);
  } catch (CORBA::TRANSIENT&) {
    std::cerr << "TRANSIENT Exception : Error on network" << std::endl
            << "Wrong server or connection malfunction" << std::endl;
  }
}

CorbaSender::~CorbaSender() {
  try{
    CORBA::release(_dataref);
  } catch (CORBA::TRANSIENT&) {
    std::cerr << "TRANSIENT Exception : Error on network" << std::endl
            << "Wrong server or connection malfunction" << std::endl;
  }
}
